package day114;

import java.sql.Connection;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
@Auther: MFM
@Date: 2021/11/4 - 11 - 04 - 18:43 
@version: 1.0
*/

public class Account {
    private boolean flag;
    private String currentThread = "甲";
    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();


    public void draw() throws  Exception{
            lock.lock();
            if (this.flag){
                if (this.currentThread.equals(Thread.currentThread().getName())){
                    System.out.println(Thread.currentThread().getName()+"取出");
                    this.flag = false;
                    switch (currentThread){
                        case "甲":
                            this.currentThread = "乙";
                            break;
                        case "乙":
                            this.currentThread = "丙";
                            break;
                        case "丙":
                            this.currentThread = "甲";
                            break;
                    }
                    Thread.sleep(1000);
                    condition.signalAll();
                }else {
                    condition.await();
                }

            }else {
                condition.await();
            }
            lock.unlock();
    }

    public void dispose() throws  Exception{
            lock.lock();
            if (this.flag){
                condition.await();
            }else {
                System.out.println(Thread.currentThread().getName()+"存入");
                this.flag = true;
                Thread.sleep(1000);
                condition.signalAll();
            }
            lock.unlock();
        }
}
